//controlls
define(function (require, exports, module) {
    const Vue=require("vue");
    var template=require('text!./index.html');
    const weui=require('weui');
    const api=require("api");
    const sdk=require("sdk");
    const utils=require('utils');
    module.exports=Vue.extend({
        template:template,
        data:function () {
            return {
                choose_amount:{},
                charge_presents:[]
            }
        },
        created:function () {

        },
        computed:{
            isBack:function () {
                return this.$store.getters.getIsBack;
            },
            oauth:function () {
                return this.$store.getters.getOAuth;
            },
            user_info:function () {
                return this.$store.getters.getUserInfo;
            }
        },
        beforeRouteEnter:function (to,from,next) {
            next(function (vm) {
                if(!vm.isBack){
                    vm.getChargePresent();
                }
            })
        },
        methods:{
            chooseAmount:function (index) {
                if(this.charge_presents.length>0){
                    this.choose_amount=this.charge_presents[index];
                }
            },
            getChargePresent:function () {
                var v=this;
                var loading = weui.loading('loading……');
                //调用后台接口 下单
                api.getChargePresent(v.user_info).then(function (data) {
                    if(data.get_charge_present&&data.get_charge_present.return_code==="SUCCESS"){
                        v.charge_presents=data.get_charge_present.charge_presents;
                        if(v.charge_presents.length>0){
                            v.choose_amount=v.charge_presents[0];
                        }
                    }else {
                        weui.topTips(data.get_charge_present.return_msg);
                    }
                }).catch(function (e) {
                    weui.topTips("网络异常，请刷新页面再试!");
                }).finally(function () {
                    loading.hide();
                });
            },
            //提交充值申请
            postDeposit:function () {
                var v=this;
                if(!v.oauth.open_id||v.oauth.open_id==="undefined"){
                    if(utils.isWeixn()) {
                        window.location.href = utils.oauthUrl();
                    }
                    else {
                        weui.topTips("请在微信中使用此功能！");
                    }
                    return;
                }
                if(!v.choose_amount.charge_amount||parseInt(v.choose_amount.charge_amount)<=0){
                    weui.topTips("请选择充值金额！");
                    return;
                }
                var loading = weui.loading('提交中……');
                //调用后台接口 下单
                api.chargeDeposit("0",v.choose_amount.charge_amount+'',v.choose_amount.present_amount||(0+''),v.oauth.open_id,v.user_info).then(function (data) {
                    if(data.user_token_invalid){
                        v.$store.commit("logOut");
                        v.$router.push({path: '/phone'});
                        return;
                    }
                    if(data.user_charge_deposit&&data.user_charge_deposit.return_code==="SUCCESS"){
                        var sdkObj=JSON.parse(data.user_charge_deposit.return_msg);
                        var adopt_seriNo=sdkObj.adopt_seriNo;
                        //拿到支付订单信息 调用支付接口
                        sdk.pay2(sdkObj).then(function (res) {
                            if(/:ok$/i.test(res.err_msg)){
                                ///充值成功提交
                                if(adopt_seriNo)
                                    api.chargeDespositStatus(adopt_seriNo,"1",res.err_msg,v.user_info);
                                v.$store.dispatch('UserInfo', v.user_info);
                                weui.toast("充值成功！");
                            }
                        }).catch(function (e) {
                            //充值失败提交
                            if(adopt_seriNo)
                               api.chargeDespositStatus(adopt_seriNo,"0",e.err_msg,v.user_info);
                            if(/:cancel$/i.test(e.err_msg)){
                                weui.toast("取消支付！");
                            }else if(/:fail/i.test(e.err_msg)){
                                weui.toast("支付失败！");
                            }
                        });
                    }else {
                        weui.topTips(data.user_charge_deposit.return_msg);
                    }
                }).catch(function (e) {
                    weui.topTips("网络异常，请刷新页面再试!");
                }).finally(function () {
                    loading.hide();
                });
            }
        },
        filters:{
            presentText:function (val) {
                if(!val)return "";
                val=parseInt(val);
                if(val>0){
                    return "送"+val+"元";
                }
                return "";
            }
        }
    });
});